package com.freewater.library.lending.reader.infrastructure.po;

import com.freewater.library.lending.reader.infrastructure.model.CheckedOutOverdueBookImpl;
import com.freewater.library.lending.reader.model.CheckedOutOverdueBook;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Table(name = "reader_checked_out_overdue")
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CheckedOutOverduePo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @JoinColumn(name = "reader_id")
    private Long readerId;

    private String qrCode;

    public CheckedOutOverduePo(Long readerId, CheckedOutOverdueBook overdueBook) {
        this.readerId = readerId;
        this.qrCode = overdueBook.getQrCode();
        if (overdueBook instanceof CheckedOutOverdueBookImpl overdueBookImpl) {
            this.id = overdueBookImpl.getDbId();
        } else {
            this.id = 0;
        }
    }

    public static CheckedOutOverdueBook convert(CheckedOutOverduePo record) {
        CheckedOutOverdueBookImpl overdueBook = new CheckedOutOverdueBookImpl(record.getQrCode());
        overdueBook.setDbId(record.getId());
        return overdueBook;
    }
}
